﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using System.Drawing.Printing;

namespace 生产计划批量打印工具
{
    public partial class frmPrintList : Form
    {
        string strConnectionString = "Data Source=@ServerAddress;Initial Catalog=@DatabaseName;Persist Security Info=True;User ID=sa;Password=856518armyman";
        string strDefaultSelectString = "select jhb001+'|'+cast(jhb002 as varchar(3)) 计划单号,hta011 合同号,jhb005 型号规格,cast(jhb008 as int) 数量,jhb009 交货日期,htb023 订单单项备注,htb032 产品单项备注,htb021+'|'+htb022 执行标准,ycl041 等级,ycl027 型号,ycl018 尺寸,dbo.GetCapacity (jhb005)容量,hta001 订单编号,htc002 订单备注 from scjha_table ,scjhb_table left join hta_table on hta001=jhb003 left join htb_table on htb001=jhb003 and htb002=jhb004 and htb001=hta001 left join ycl_table on ycl001=jhb005 left join htc_table on htc001=hta001 where jha001=jhb001 and jha007='Y' and hta011 not like '%送样%' and hta011 not like '%宏科片%' and jha002='@计划日期' and jhb005 like '@型号规格' order by ycl041,ycl027 ,ycl018,dbo.GetCapacity (jhb005),jhb009";
        SqlTools.SqlTools sqt;

        string strFactoryName;

        public frmPrintList(string strTmpFactoryName)
        {
            InitializeComponent();
            strFactoryName = strTmpFactoryName;
            switch (strFactoryName)
            {
                case "601车间":
                case "微波车间":
                    strConnectionString = strConnectionString.Replace("@DatabaseName", "TPAMainTZ");
                    break;
                case "603车间":
                    strConnectionString = strConnectionString.Replace("@DatabaseName", "TPAMain");
                    break;
            }
        }

        private string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString().Trim();
            }
            catch
            {
                return string.Empty;
            }
        }

        private void frmPrintList_Load(object sender, EventArgs e)
        {
            strConnectionString = strConnectionString.Replace("@ServerAddress", 生产计划批量打印工具.Properties.Settings.Default.ServerAddress);
            sqt = new SqlTools.SqlTools(strConnectionString);
        }

        private void chkQueryModel_CheckedChanged(object sender, EventArgs e)
        {
            txtModel.Enabled = chkQueryModel.Checked;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string strTmpSelectString = strDefaultSelectString;
            strTmpSelectString = strTmpSelectString.Replace("@计划日期", dtp1.Value.ToString("yyyyMMdd"));
            if (chkQueryModel.Checked)
            {
                strTmpSelectString = strTmpSelectString.Replace("@型号规格", txtModel.Text);
            }
            else
            {
                strTmpSelectString = strTmpSelectString.Replace("@型号规格", "%");
            }
            dgv1.DataSource = sqt.GetDataTable(strTmpSelectString);
            dgv1.AutoResizeColumns();
            dgv1.Columns["订单编号"].Visible = false;
            dgv1.Columns["订单备注"].Visible = false;
        }

        private void frmPrintList_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Owner.Show();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (!chkPrintSelected.Checked)
            {
                dgv1.SelectAll();
            }
            string strDefaultPrinter = GetDefaultPrinter();
            string strPrinter = string.Empty;
            if (dgv1.SelectedRows.Count > 0)
            {
                #region 打印生产计划清单
                ReportDocument rd = new ReportDocument();
                try
                {
                    DataTable dt_qz = new DataTable("ReportData");
                    dt_qz.Columns.Add("标题");
                    dt_qz.Columns.Add("合同号");
                    dt_qz.Columns.Add("计划单号");
                    dt_qz.Columns.Add("型号规格");
                    dt_qz.Columns.Add("数量");
                    dt_qz.Columns.Add("订单单项备注");
                    dt_qz.Columns.Add("产品单项备注");
                    dt_qz.Columns.Add("交货日期");
                    dt_qz.Columns.Add("执行标准");
                    dt_qz.Columns.Add("等级");
                    dt_qz.Columns.Add("型号");
                    dt_qz.Columns.Add("尺寸");
                    dt_qz.Columns.Add("容量", typeof(double));
                    DataTable dt_pj = new DataTable("ReportData");
                    dt_pj.Columns.Add("标题");
                    dt_pj.Columns.Add("合同号");
                    dt_pj.Columns.Add("计划单号");
                    dt_pj.Columns.Add("型号规格");
                    dt_pj.Columns.Add("数量");
                    dt_pj.Columns.Add("订单单项备注");
                    dt_pj.Columns.Add("产品单项备注");
                    dt_pj.Columns.Add("交货日期");
                    dt_pj.Columns.Add("执行标准");
                    dt_pj.Columns.Add("等级");
                    dt_pj.Columns.Add("型号");
                    dt_pj.Columns.Add("尺寸");
                    dt_pj.Columns.Add("容量", typeof(double));
                    for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                    {
                        DataRow dr;
                        bool bIsQz = false;
                        if (GetString(dgv1.SelectedRows[i].Cells["型号规格"].Value).StartsWith("M-") ||
                            GetString(dgv1.SelectedRows[i].Cells["型号规格"].Value).StartsWith("J-"))
                        {
                            dr = dt_pj.NewRow();
                        }
                        else
                        {
                            dr = dt_qz.NewRow();
                            bIsQz = true;
                        }
                        if (bIsQz)
                        {
                            dr["标题"] = dtp1.Value.ToString("yyyy年M月d日七专生产计划清单");
                        }
                        if (!bIsQz)
                        {
                            dr["标题"] = dtp1.Value.ToString("yyyy年M月d日普军生产计划清单");
                        }
                        dr["合同号"] = dgv1.SelectedRows[i].Cells["合同号"].Value;
                        dr["计划单号"] = dgv1.SelectedRows[i].Cells["计划单号"].Value;
                        dr["型号规格"] = dgv1.SelectedRows[i].Cells["型号规格"].Value;
                        dr["数量"] = dgv1.SelectedRows[i].Cells["数量"].Value;
                        dr["订单单项备注"] = dgv1.SelectedRows[i].Cells["订单单项备注"].Value;
                        dr["产品单项备注"] = dgv1.SelectedRows[i].Cells["产品单项备注"].Value;
                        dr["交货日期"] = dgv1.SelectedRows[i].Cells["交货日期"].Value;
                        dr["执行标准"] = dgv1.SelectedRows[i].Cells["执行标准"].Value;
                        dr["等级"] = dgv1.SelectedRows[i].Cells["等级"].Value;
                        dr["型号"] = dgv1.SelectedRows[i].Cells["型号"].Value;
                        dr["尺寸"] = dgv1.SelectedRows[i].Cells["尺寸"].Value;
                        dr["容量"] = dgv1.SelectedRows[i].Cells["容量"].Value;
                        if (bIsQz)
                        {
                            dt_qz.Rows.Add(dr);
                        }
                        else
                        {
                            dt_pj.Rows.Add(dr);
                        }
                    }
                    if (chkQz.Checked&&dt_qz.Rows.Count >0 )
                    {
                        DataView dv = dt_qz.DefaultView;
                        dv.Sort = "等级,型号,尺寸,容量";
                        DataTable tmpdt = dv.ToTable();
                        rd.Load(Application.StartupPath + "\\计划清单.rpt");
                        rd.SetDataSource(tmpdt );
                        if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                        {
                            rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                            strPrinter = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                        }
                        else
                        {
                            strPrinter = strDefaultPrinter;
                        }
                        rd.PrintToPrinter((int) numericUpDown1 .Value , true, 0, 0);
                    }
                    if (chkPj.Checked &&dt_pj .Rows.Count >0)
                    {
                        DataView dv = dt_pj.DefaultView;
                        dv.Sort = "等级,型号,尺寸,容量";
                        DataTable tmpdt = dv.ToTable();
                        rd.Load(Application.StartupPath + "\\计划清单.rpt");
                        rd.SetDataSource(tmpdt);
                        if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                        {
                            rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                            strPrinter = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                        }
                        else
                        {
                            strPrinter = strDefaultPrinter;
                        }
                        rd.PrintToPrinter((int)numericUpDown1.Value, true, 0, 0);
                    }
                }
                catch
                {
                }
                finally
                {
                    rd.Dispose();
                }
                #endregion
                #region 打印订单备注清单
                rd = new ReportDocument();
                try
                {
                    DataTable dt_qz = new DataTable("ReportData");
                    dt_qz = new DataTable("ReportData");
                    dt_qz.Columns.Add("标题");
                    dt_qz.Columns.Add("合同号");
                    dt_qz.Columns.Add("订单编号");
                    dt_qz.Columns.Add("备注");
                    DataTable dt_pj = new DataTable("ReportData");
                    dt_pj = new DataTable("ReportData");
                    dt_pj.Columns.Add("标题");
                    dt_pj.Columns.Add("合同号");
                    dt_pj.Columns.Add("订单编号");
                    dt_pj.Columns.Add("备注");
                    for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                    {
                        DataRow dr;
                        bool bIsQz = false;
                        if (GetString(dgv1.SelectedRows[i].Cells["型号规格"].Value).StartsWith("M-") ||
                            GetString(dgv1.SelectedRows[i].Cells["型号规格"].Value).StartsWith("J-"))
                        {
                            dr = dt_pj.NewRow();
                        }
                        else
                        {
                            dr = dt_qz.NewRow();
                            bIsQz = true;
                        }
                        if (bIsQz)
                        {
                            dr["标题"] = dtp1.Value.ToString("yyyy年M月d日七专订单备注清单");
                        }
                        if (!bIsQz)
                        {
                            dr["标题"] = dtp1.Value.ToString("yyyy年M月d日普军订单备注清单");
                        }
                        dr["合同号"] = dgv1.SelectedRows[i].Cells["合同号"].Value;
                        dr["订单编号"] = dgv1.SelectedRows[i].Cells["订单编号"].Value;
                        dr["备注"] = dgv1.SelectedRows[i].Cells["订单备注"].Value;
                        if (bIsQz)
                        {
                            bool bOrderExist = false;
                            for (int k = 0; k < dt_qz.Rows.Count; k++)
                            {
                                if (dr["订单编号"].Equals(dt_qz.Rows[k]["订单编号"]))
                                {
                                    bOrderExist = true;
                                    break;
                                }
                            }
                            if (!bOrderExist)
                            {
                                dt_qz.Rows.Add(dr);
                            }
                        }
                        else
                        {
                            bool bOrderExist = false;
                            for (int k = 0; k < dt_pj.Rows.Count; k++)
                            {
                                if (dr["订单编号"].Equals(dt_pj.Rows[k]["订单编号"]))
                                {
                                    bOrderExist = true;
                                    break;
                                }
                            }
                            if (!bOrderExist)
                            {
                                dt_pj.Rows.Add(dr);
                            }
                        }
                    }
                    if (chkQz.Checked && dt_qz.Rows.Count > 0)
                    {
                        rd.Load(Application.StartupPath + "\\订单备注清单.rpt");
                        rd.SetDataSource(dt_qz);
                        if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                        {
                            rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                            strPrinter = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                        }
                        else
                        {
                            strPrinter = strDefaultPrinter;
                        }
                        rd.PrintToPrinter((int)numericUpDown1.Value, true, 0, 0);
                    }
                    if (chkPj.Checked && dt_pj.Rows.Count > 0)
                    {
                        rd.Load(Application.StartupPath + "\\订单备注清单.rpt");
                        rd.SetDataSource(dt_pj);
                        if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                        {
                            rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                            strPrinter = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                        }
                        else
                        {
                            strPrinter = strDefaultPrinter;
                        }
                        rd.PrintToPrinter((int)numericUpDown1.Value, true, 0, 0);
                    }
                }
                catch
                {
                }
                finally
                {
                    rd.Dispose();
                }
                #endregion
            }
            else
            {
                throw new Exception("未选择任何生产计划！");
            }
        }

        /// <summary>
        /// 获取默认打印机
        /// </summary>
        /// <returns>默认打印机名称</returns>
        private string GetDefaultPrinter()
        {
            PrintDocument pd = new PrintDocument();
            return pd.PrinterSettings.PrinterName;
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            if (!chkPrintSelected.Checked)
            {
                dgv1.SelectAll();
            }
            string strDefaultPrinter = GetDefaultPrinter();
            string strPrinter = string.Empty;
            if (dgv1.SelectedRows.Count > 0)
            {
                ReportDocument rd = new ReportDocument();
                #region 预览生产计划清单
                try
                {
                    DataTable dt_qz = new DataTable("ReportData");
                    dt_qz.Columns.Add("标题");
                    dt_qz.Columns.Add("合同号");
                    dt_qz.Columns.Add("计划单号");
                    dt_qz.Columns.Add("型号规格");
                    dt_qz.Columns.Add("数量");
                    dt_qz.Columns.Add("订单单项备注");
                    dt_qz.Columns.Add("产品单项备注");
                    dt_qz.Columns.Add("交货日期");
                    dt_qz.Columns.Add("执行标准");
                    dt_qz.Columns.Add("等级");
                    dt_qz.Columns.Add("型号");
                    dt_qz.Columns.Add("尺寸");
                    dt_qz.Columns.Add("容量",typeof (double ));
                    DataTable dt_pj = new DataTable("ReportData");
                    dt_pj.Columns.Add("标题");
                    dt_pj.Columns.Add("合同号");
                    dt_pj.Columns.Add("计划单号");
                    dt_pj.Columns.Add("型号规格");
                    dt_pj.Columns.Add("数量");
                    dt_pj.Columns.Add("订单单项备注");
                    dt_pj.Columns.Add("产品单项备注");
                    dt_pj.Columns.Add("交货日期");
                    dt_pj.Columns.Add("执行标准");
                    dt_pj.Columns.Add("等级");
                    dt_pj.Columns.Add("型号");
                    dt_pj.Columns.Add("尺寸");
                    dt_pj.Columns.Add("容量",typeof (double ));
                    for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                    {
                        DataRow dr;
                        bool bIsQz = false;
                        if (GetString(dgv1.SelectedRows[i].Cells["型号规格"].Value).StartsWith("M-") ||
                            GetString(dgv1.SelectedRows[i].Cells["型号规格"].Value).StartsWith("J-"))
                        {
                            dr = dt_pj.NewRow();
                        }
                        else
                        {
                            dr = dt_qz.NewRow();
                            bIsQz = true;
                        }
                        if (bIsQz)
                        {
                            dr["标题"] = dtp1.Value.ToString("yyyy年M月d日七专生产计划清单");
                        }
                        if (!bIsQz)
                        {
                            dr["标题"] = dtp1.Value.ToString("yyyy年M月d日普军生产计划清单");
                        }
                        dr["合同号"] = dgv1.SelectedRows[i].Cells["合同号"].Value;
                        dr["计划单号"] = dgv1.SelectedRows[i].Cells["计划单号"].Value;
                        dr["型号规格"] = dgv1.SelectedRows[i].Cells["型号规格"].Value;
                        dr["数量"] = dgv1.SelectedRows[i].Cells["数量"].Value;
                        dr["订单单项备注"] = dgv1.SelectedRows[i].Cells["订单单项备注"].Value;
                        dr["产品单项备注"] = dgv1.SelectedRows[i].Cells["产品单项备注"].Value;
                        dr["交货日期"] = dgv1.SelectedRows[i].Cells["交货日期"].Value;
                        dr["执行标准"] = dgv1.SelectedRows[i].Cells["执行标准"].Value;
                        dr["等级"] = dgv1.SelectedRows[i].Cells["等级"].Value;
                        dr["型号"] = dgv1.SelectedRows[i].Cells["型号"].Value;
                        dr["尺寸"] = dgv1.SelectedRows[i].Cells["尺寸"].Value;
                        dr["容量"] = dgv1.SelectedRows[i].Cells["容量"].Value;
                        if (bIsQz)
                        {
                            dt_qz.Rows.Add(dr);
                        }
                        else
                        {
                            dt_pj.Rows.Add(dr);
                        }
                    }
                    if (chkQz.Checked && dt_qz.Rows.Count > 0)
                    {
                        DataView dv = dt_qz.DefaultView;
                        dv.Sort = "等级,型号,尺寸,容量";
                        DataTable tmpdt = dv.ToTable();
                        rd.Load(Application.StartupPath + "\\计划清单.rpt");
                        rd.SetDataSource(tmpdt);
                        frmReportPreview newFrmReportPreview = new frmReportPreview(rd);
                        newFrmReportPreview.ShowDialog();
                    }
                    if (chkPj.Checked && dt_pj.Rows.Count > 0)
                    {
                        DataView dv = dt_pj.DefaultView;
                        dv.Sort = "等级,型号,尺寸,容量";
                        DataTable tmpdt = dv.ToTable();
                        rd.Load(Application.StartupPath + "\\计划清单.rpt");
                        rd.SetDataSource(tmpdt);
                        frmReportPreview newFrmReportPreview = new frmReportPreview(rd);
                        newFrmReportPreview.ShowDialog();
                    }
                }
                catch
                {
                }
                finally
                {
                    rd.Dispose();
                }
                #endregion
                #region 预览订单备注清单
                rd = new ReportDocument();
                try
                {
                    DataTable dt_qz = new DataTable("ReportData");
                    dt_qz = new DataTable("ReportData");
                    dt_qz.Columns.Add("标题");
                    dt_qz.Columns.Add("合同号");
                    dt_qz.Columns.Add("订单编号");
                    dt_qz.Columns.Add("备注");
                    DataTable dt_pj = new DataTable("ReportData");
                    dt_pj = new DataTable("ReportData");
                    dt_pj.Columns.Add("标题");
                    dt_pj.Columns.Add("合同号");
                    dt_pj.Columns.Add("订单编号");
                    dt_pj.Columns.Add("备注");
                    for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                    {
                        DataRow dr;
                        bool bIsQz = false;
                        if (GetString(dgv1.SelectedRows[i].Cells["型号规格"].Value).StartsWith("M-") ||
                            GetString(dgv1.SelectedRows[i].Cells["型号规格"].Value).StartsWith("J-"))
                        {
                            dr = dt_pj.NewRow();
                        }
                        else
                        {
                            dr = dt_qz.NewRow();
                            bIsQz = true;
                        }
                        if (bIsQz)
                        {
                            dr["标题"] = dtp1.Value.ToString("yyyy年M月d日七专订单备注清单");
                        }
                        if (!bIsQz)
                        {
                            dr["标题"] = dtp1.Value.ToString("yyyy年M月d日普军订单备注清单");
                        }
                        dr["合同号"] = dgv1.SelectedRows[i].Cells["合同号"].Value;
                        dr["订单编号"] = dgv1.SelectedRows[i].Cells["订单编号"].Value;
                        dr["备注"] = dgv1.SelectedRows[i].Cells["订单备注"].Value;
                        if (bIsQz)
                        {
                            bool bOrderExist = false;
                            for (int k = 0; k < dt_qz.Rows.Count; k++)
                            {
                                if (dr["订单编号"].Equals(dt_qz.Rows[k]["订单编号"]))
                                {
                                    bOrderExist = true;
                                    break;
                                }
                            }
                            if (!bOrderExist)
                            {
                                dt_qz.Rows.Add(dr);
                            }
                        }
                        else
                        {
                            bool bOrderExist = false;
                            for (int k = 0; k < dt_pj.Rows.Count; k++)
                            {
                                if (dr["订单编号"].Equals(dt_pj.Rows[k]["订单编号"]))
                                {
                                    bOrderExist = true;
                                    break;
                                }
                            }
                            if (!bOrderExist)
                            {
                                dt_pj.Rows.Add(dr);
                            }
                        }
                    }
                    if (chkQz.Checked && dt_qz.Rows.Count > 0)
                    {
                        rd.Load(Application.StartupPath + "\\订单备注清单.rpt");
                        rd.SetDataSource(dt_qz);
                        frmReportPreview newFrmReportPreview = new frmReportPreview(rd);
                        newFrmReportPreview.ShowDialog();
                    }
                    if (chkPj.Checked && dt_pj.Rows.Count > 0)
                    {
                        rd.Load(Application.StartupPath + "\\订单备注清单.rpt");
                        rd.SetDataSource(dt_pj);
                        frmReportPreview newFrmReportPreview = new frmReportPreview(rd);
                        newFrmReportPreview.ShowDialog();
                    }
                }
                catch
                {
                }
                finally
                {
                    rd.Dispose();
                }
                #endregion
            }
            else
            {
                throw new Exception("未选择任何生产计划！");
            }
        }
    }
}